package com.smec.moblie.util;

import android.content.Context;
import android.os.Environment;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/***
 * 文件处理类
 *
 * @author xinwei.sun
 */
public class FileUtil {

    private Context context;

    public FileUtil(Context context) {
        this.context = context;
    }

    public FileUtil() {
    }

    @SuppressWarnings("resource")
    public String getInputstream(String filename) {
        FileInputStream fileInputStream = null;
        ByteArrayOutputStream OutputStream = new ByteArrayOutputStream();
        File file = new File(Environment.getExternalStorageDirectory(), filename);
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            try {
                fileInputStream = new FileInputStream(file);
                int len = 0;
                byte[] data = new byte[1024];
                while ((len = fileInputStream.read(data)) != -1) {
                    OutputStream.write(data, 0, len);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileInputStream != null) {
                    fileInputStream = null;
                }
            }

        }

        return new String(OutputStream.toByteArray());
    }

    /**
     * @param Filename 文件名
     * @param content
     * @return
     */
    public boolean saveContentTosdCard(String Filename, String content) {
        boolean flag = false;
        FileOutputStream outputStream = null;
        File file = new File(Environment.getExternalStorageDirectory(), Filename);
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            try {
                outputStream = new FileOutputStream(file);
                outputStream.write(content.getBytes());
                flag = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return flag;
    }

    /**
     * 读取SD卡中文本文件
     *
     * @param fileName
     * @return
     */
    public static String readSDFile(String fileName) {
//        StringBuffer sb = new StringBuffer();
        String ret = null;
        File file = new File(Environment.getExternalStorageDirectory() + "//" + fileName);
//        try {
//            FileInputStream fis = new FileInputStream(file);
//            int c;
//            while ((c = fis.read()) != -1) {
//                sb.append((char) c);
//            }
//            fis.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        ret = readFile(file.getPath());
        return ret;
    }

    /**

     * 读取文件内容

     *

     * @param filePathAndName

     * @return

     */

    public static String readFile(String filePathAndName) {

        String fileContent = null;

        try {

            File f = new File(filePathAndName);

            if (f.isFile() && f.exists()) {

                fileContent = "";

                InputStreamReader read = new InputStreamReader(

                        new FileInputStream(f), "UTF-8");

                BufferedReader reader = new BufferedReader(read);

                String line;

                while ((line = reader.readLine()) != null) {

                    fileContent += line;

                }

                read.close();

            }

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }

        return fileContent;

    }

}
